// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TimerModelAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Visualization
{
    using System;
    using System.Runtime.Serialization;
    using Microsoft.Robotics.Runtime;

    /// <summary>
    /// This model agent class facilitates the display of the timer info and enables pause/resume/skip control over the timer.
    /// </summary>
    [DataContract]
    public sealed class TimerModelAgent : ModelAgent<TimerMessage, TimerModel>
    {
        /// <summary>
        /// Initializes a new instance of the TimerModelAgent class
        /// </summary>
        /// <param name="name">Agent name</param>
        /// <param name="producerAgent">The name of the agent we subscribe to.</param>
        public TimerModelAgent(string name, IProducer<TimerMessage> producerAgent)
            : base(name, producerAgent.Name, 1)
        {
        }

        /// <summary>
        /// Called to finish initialization after the constructor has finished.
        /// </summary>
        /// <param name="locator">Locator to use.</param>
        public override void Initialize(AgentLocator locator)
        {
            base.Initialize(locator);

            IForwarder<TimerControlMessage> controlPort = this.ProducerAgent.GetControlPort<TimerControlMessage>();
            this.Model.PauseSkipSeekDelegate = (pause, skip, seek, seekTime) => controlPort.Post(new TimerControlMessage(pause, skip, seek, seekTime));         
        }

        /// <summary>
        /// Updates the model data with the new state.
        /// </summary>
        /// <param name="message">The new message from the upstream agent.</param>
        /// <returns>true if the model was updated, false otherwise</returns>
        protected override bool UpdateModel(TimerMessage message)
        {
            this.Model.ElapsedTime = TimeSpan.FromMilliseconds(message.RelativeTime);

            return true;
        }
    }
}
